package com.huibao.hulibao.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextPaint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.huibao.hulibao.R;
import com.huibao.hulibao.activity.BigPicActivity;
import com.huibao.hulibao.activity.MainActivity;
import com.huibao.hulibao.activity.StartHuliActivity;
import com.huibao.hulibao.activity.WebViewActivity;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.HuLiLogEntity;
import com.huibao.hulibao.entity.LoginEntity;
import com.huibao.hulibao.entity.WebEntity;
import com.huibao.hulibao.utils.MyUtils;
import com.huibao.hulibao.utils.PreUtils;
import com.huibao.hulibao.utils.T;
import com.nex3z.flowlayout.FlowLayout;

import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class StartHuliLvAdapter extends BaseAdapter {
    private List<HuLiLogEntity.LogDetailData> mDatas;
    private Context mContext;

    private LayoutInflater mInflater;

    private String pic;
    private StartHuliActivity sha;

    public StartHuliLvAdapter(List<HuLiLogEntity.LogDetailData> datas, Context context, String pic, StartHuliActivity sha) {
        this.mDatas = datas;
        this.mContext = context;
        this.pic = pic;
        this.sha = sha;

        mInflater = LayoutInflater.from(context);
    }

    public void jumpToWebView(String url) {

        LoginEntity le = (LoginEntity) PreUtils.getObject(mContext, "LoginEntity");
        if (le == null) {
            return;
        }
        Intent intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra("url", GlobalConstants.HOST + url + le.Message.Session);
        // intent.putExtra("url", "https://www.baidu.com/");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("title", mContext.getResources().getString(R.string.app_name));
        mContext.startActivity(intent);

    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final HuLiLogEntity.LogDetailData ldd = mDatas.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_start_huli, parent, false);
            holder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        String data = ldd.m2.split("\\|")[0].split("\\s")[0];
        String time = ldd.m2.split("\\|")[0].split("\\s")[1];

        // 加粗和是否显示今天
        TextPaint tp = holder.tv_lv_time.getPaint();
        try {
            if (MyUtils.isToday(data, "yyyy-MM-dd")) {
                holder.tv_lv_time.setText("今天 " + time);
                tp.setFakeBoldText(true);
            } else {
                holder.tv_lv_time.setText(ldd.m2.split("\\|")[0]);
                tp.setFakeBoldText(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        // 是否显示可修改
        if (ldd.CanModify.equals("1") && sha.nid.equals(ldd.tid)) {
            holder.tv_change.setVisibility(View.VISIBLE);
        } else {
            holder.tv_change.setVisibility(View.INVISIBLE);
        }

        holder.tv_name.setText(ldd.m2.split("\\|")[1]);
        holder.tv_bei_zhu.setText(ldd.m3);
        holder.tv_lv_item_record.setText(ldd.tname.trim());


        if (ldd.IconUrl != null && !ldd.IconUrl.equals("")) {
            Glide.with(mContext).load(GlobalConstants.HOST + ldd.IconUrl).into(holder.iv_lv_start_icon);
        } else {
            holder.iv_lv_start_icon.setImageResource(R.mipmap.huli_gv_defult_mom);
        }

        // 加载显示图片
        if (ldd.imgs.equals("")) {
            holder.tv_pic_number.setVisibility(View.GONE);
        } else {
            holder.tv_pic_number.setVisibility(View.VISIBLE);
            String[] arr = ldd.imgs.split("\\|");
            // int num = arr.length;
            holder.tv_pic_number.setText("[有" + arr.length + "图片]");
        }

        holder.tv_pic_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToWebView(ldd.ImgShowTools);
            }
        });


        holder.tv_chakanxiangqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sha.jumpWeb();
            }
        });


        holder.tv_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sha.getSingleMsgFromServer(ldd.rid);
            }
        });

//        if (pic != null && !pic.equals("")) {
//            Glide.with(mContext).load(GlobalConstants.HOST + pic).into(holder.iv_lv_start_icon);
//        }

        // LogUtils.d("pic = " + GlobalConstants.HOST + pic);
        ArrayList<String> al = new ArrayList<>();

        String[] arr = ldd.m1.split("\\|");
        if (arr.length > 0) {
            holder.tv_huli_ren.setText(arr[0]);
            for (int i = 0; i < arr.length; i++) {
                if (i > 0) {
                    al.add(arr[i]);
                }
            }
        }

        holder.gv_lv.setAdapter(new StartHuLiGvAdapter(mContext, al));
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.tv_lv_item_record)
        TextView tv_lv_item_record;
        @BindView(R.id.tv_huli_ren)
        TextView tv_huli_ren;
        @BindView(R.id.tv_bei_zhu)
        TextView tv_bei_zhu;
        @BindView(R.id.tv_lv_time)
        TextView tv_lv_time;
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.gv_lv)
        GridView gv_lv;
        @BindView(R.id.iv_lv_start_icon)
        ImageView iv_lv_start_icon;
        @BindView(R.id.flow_category)
        FlowLayout flow_category;
        @BindView(R.id.tv_pic_number)
        TextView tv_pic_number;
        @BindView(R.id.tv_change)
        TextView tv_change;
        @BindView(R.id.tv_chakanxiangqing)
        TextView tv_chakanxiangqing;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
